10. Voxel Grid Downsampling

Voxel Grid Downsampling

Front view of a scene on the left and voxel grid overlaid on the point cloud of the same scene on the right.

Front view of a scene on the left and voxel grid overlaid on the point cloud of the same scene on the right.

RGB-D cameras provide feature rich and particularly dense point clouds, meaning, more points are packed in per unit volume than, for example, a Lidar point cloud. Running computation on a full resolution point cloud can be slow and may not yield any improvement on results obtained using a more sparsely sampled point cloud.

So, in many cases, it is advantageous to downsample the data. In particular, you are going to use a VoxelGrid Downsampling Filter to derive a point cloud that has fewer points but should still do a good job of representing the input point cloud as a whole.

The word "pixel" is short for "picture element". Similarly, the word "voxel" is short for "volume element". Just as you can divide the 2D image into a regular grid of area elements, as shown in the image on the left above, you can divide up your 3D point cloud, into a regular 3D grid of volume elements as shown on the right. Each individual cell in the grid is now a voxel and the 3D grid is known as a "voxel grid".

A voxel grid filter allows you to downsample the data by taking a spatial average of the points in the cloud confined by each voxel. You can adjust the sampling size by setting the voxel size along each dimension. The set of points which lie within the bounds of a voxel are assigned to that voxel and statistically combined into one output point.

PCL provides a handy function to perform VoxelGrid downsampling. Add the following code to the Voxel Grid Downsampling section of RANSAC.py.

# Create a VoxelGrid filter object for our input point cloud
vox = cloud.make_voxel_grid_filter()

# Choose a voxel (also known as leaf) size
# Note: this (1) is a poor choice of leaf size   
# Experiment and find the appropriate size!
LEAF_SIZE = 1   

# Set the voxel (or leaf) size  
vox.set_leaf_size(LEAF_SIZE, LEAF_SIZE, LEAF_SIZE)

# Call the filter function to obtain the resultant downsampled point cloud
cloud_filtered = vox.filter()
filename = 'voxel_downsampled.pcd'
pcl.save(cloud_filtered, filename)

Now you can run the file using Python and view the result using the pcl_viewer

$ python RANSAC.py
$ pcl_viewer voxel_downsampled.pcd 

Note: You can use your mouse to rotate and zoom in/out the viewpoint of the scene/point cloud. Press r to center the entire point cloud.

The units of the voxel size (or leaf size) are in meters. Setting it to 1 implies your voxel is 1 cubic meter in volume, which might not produce the best results as it could remove important features. So, in practice, you should start out with a small voxel size and then increment to larger sizes until you reach the point that any further increase would cause the loss of important features. A good estimate of the voxel size can be obtained by having some prior information about the scene like size of the smallest objects, size of the target object, or total volume of the scene in Field of View.

Meaning of voxel size

Generally speaking, does downsampling to a larger or smaller voxel size retain more information about the original point cloud?

SOLUTION: Smaller

Voxel Size

After experimenting a bit, which of the following might be considered a reasonable voxel (leaf) size for this dataset?

SOLUTION: 0.01